package com.example.baidu_map_plugin.location;


import androidx.annotation.NonNull;


import com.baidu.location.BDLocation;
import com.example.baidu_map_plugin.BaiduBasePlugin;

import java.util.HashMap;

import io.flutter.embedding.engine.plugins.FlutterPlugin;

import io.flutter.embedding.engine.plugins.shim.ShimPluginRegistry;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;

public class BaiduLocationPlugin extends BaiduBasePlugin implements BaiduLocation.LocationCallBack {
    private static final String TAG = BaiduLocationPlugin.class.getSimpleName();
    private static final String LOCATION_CHANNEL_NAME = "baidu_location_channel";
    private final BaiduLocation baiduLocation;
    private MethodChannel.Result result;

    public BaiduLocationPlugin(ShimPluginRegistry shimPluginRegistry, FlutterPlugin.FlutterPluginBinding binding, MethodChannel.MethodCallHandler methodCallHandler) {
        super(binding.getBinaryMessenger(), methodCallHandler);

        final String key = BaiduLocationPlugin.class.getCanonicalName();
        if (shimPluginRegistry.hasPlugin(key)) {
            PluginRegistry.Registrar registrarFor = shimPluginRegistry.valuePublishedByPlugin(key);
            baiduLocation = new BaiduLocation(registrarFor, this);
        } else {
            PluginRegistry.Registrar registrarFor = shimPluginRegistry.registrarFor(key);
            baiduLocation = new BaiduLocation(registrarFor, this);

        }

    }

    public BaiduLocationPlugin(PluginRegistry.Registrar registrar, MethodChannel.MethodCallHandler methodCallHandler) {
        super(registrar.messenger(), methodCallHandler);

        baiduLocation = new BaiduLocation(registrar, this);

    }

    @Override
    public String initChannelName() {
        return LOCATION_CHANNEL_NAME;
    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        this.result = result;
        switch (call.method) {
            case "getLocation":
                baiduLocation.startLocation();
                break;
        }
    }

    @Override
    public void onLocationCallBack(BDLocation location) {
        //此处的BDLocation为定位结果信息类，通过它的各种get方法可获取定位相关的全部结果
        //以下只列举部分获取经纬度相关（常用）的结果信息
        //更多结果信息获取说明，请参照类参考中BDLocation类中的说明
        //获取纬度信息
        double latitude = location.getLatitude();
        //获取经度信息
        double longitude = location.getLongitude();
        //获取定位精度，默认值为0.0f
        float radius = location.getRadius();
        //获取经纬度坐标类型，以LocationClientOption中设置过的坐标类型为准
        String coorType = location.getCoorType();
        //获取定位类型、定位错误返回码，具体信息可参照类参考中BDLocation类中的说明
        int errorCode = location.getLocType();
        if (result != null) {
            HashMap<String, Double> locationModel = new HashMap<>();
            locationModel.put("latitude", latitude);
            locationModel.put("longitude", longitude);
            result.success(locationModel);
            result = null;
        }
    }


}
